package domain

import (
	"AcsGo/internal/util"
	"AcsGo/logger"
	"fmt"
)

type LoginRequest struct {
	Code          util.Field
	UID           util.Field
	PWD           util.Field
	LoginUserID   util.FieldPrefix
	LoginPassword util.FieldPrefix
	LocationCode  util.FieldPrefix
}

var lg = logger.InitLog()

func HandleLoginRequest(str string) (*LoginRequest, error) {
	if str == "" {
		return nil, fmt.Errorf("str is empty")
	}
	var query = LoginRequest{
		Code:          util.Field{Value: "93", Length: 2},
		UID:           util.Field{Value: "", Length: 1},
		PWD:           util.Field{Value: "", Length: 1},
		LoginUserID:   util.FieldPrefix{Value: "", Prefix: "CN", Suffix: util.PrefixLink},
		LoginPassword: util.FieldPrefix{Value: "", Prefix: "CO", Suffix: util.PrefixLink},
		LocationCode:  util.FieldPrefix{Value: "", Prefix: "AY", Suffix: util.PrefixLink},
	}

	length := query.Code.Length + query.UID.Length + query.PWD.Length
	if len(str) < length {
		return nil, fmt.Errorf("str is too short")
	}

	strTemp := query.Code.GetValue(str)
	strTemp = query.UID.GetValue(strTemp)
	strTemp = query.PWD.GetValue(strTemp)
	strTemp = query.LoginUserID.GetValue(strTemp)
	strTemp = query.LoginPassword.GetValue(strTemp)
	_ = query.LocationCode.GetValue(strTemp)
	return &query, nil
}

type LoginResponse struct {
	Code          util.Field
	OK            util.Field
	ScreenMessage util.FieldPrefix
	Validate      util.FieldPrefix
}

func (q LoginResponse) StringFormat() string {
	sb, err := util.ConcatenateFields(q)
	if err != nil {
		lg.Errorf("InitLoginResponse error: %v", err)
	}
	return sb
}

func InitLoginResponse(screenMessage string) *LoginResponse {
	var queryResponse = LoginResponse{
		Code:          util.Field{Value: "94", Length: 2},
		OK:            util.Field{Value: "1", Length: 1},
		ScreenMessage: util.FieldPrefix{Value: "", Prefix: "AF", Suffix: util.PrefixLink},
		Validate:      util.FieldPrefix{Value: "1AZf5f5", Prefix: "AY", Suffix: util.PrefixLink},
	}
	if screenMessage == "" {
		queryResponse.ScreenMessage.Value = "登录成功"
	}
	return &queryResponse
}
